<template>
  <el-dialog title="分配权限" :visible="isShow" @close="closeEvent">
    <!-- 树形控件 -->
    <!--
 show-checkbox 显示复选框
 check-strictly 复选与父级无关联
 :default-checked-keys="defaultKeys" 默认勾选显示的数据[数组]
node-key="id" 指定选中该项的后的值
this.$refs.tree.setCheckedKeys()设置勾选项
this.$refs.tree.getCheckedKeys()获取勾选的数据
     -->
    <el-tree
      ref="tree"
      :data="treeList"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      :default-checked-keys="defaultKeys"
      node-key="id"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission.js'
import { changeData } from '@/utils/index'
import { sysRoleByid, sysRoleAssignPrem } from '@/api/setting.js'
export default {
  name: '',
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      treeList: [],
      defaultKeys: [],
      id: ''
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    // 权限列表数据
    async getData() {
      const res = await sysPermission()
      this.treeList = changeData(res.data, '0')
      console.log(res, '权限列表数据')
    },
    closeEvent() {
      this.$emit('update:isShow', false)
      // 关闭时置空，防止累加
      this.$refs.tree.setCheckedKeys([])
    },
    async getRoleInfo(row) {
      this.id = row.id
      // 获取角色详情，permIds
      const res = await sysRoleByid(row.id)
      // el-tree内置方法this.$refs.tree.setCheckedKeys 回显勾选状态
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log(res)
    },
    // 点击确定，，获取勾选状态，调接口，关闭弹框
    async submit() {
      sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('分配权限成功')
      this.closeEvent()
    }
  }
}
</script>
<style lang="less" scoped></style>
